using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Modules.Abstracts;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Models;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;
using FiltronASP.Helpers.Constants;
using FiltronASP.Helpers.Translations;

namespace FiltronASP.Modules
{
    public class newsSingleMonth : AbstractModule
    {
        private Page currentPage;

        /// <summary>
        /// Klasa odpowiedzialna za generowanie contentu przez modul.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <returns>content</returns>
        public override string GenerateContent(FiltronASP.Models.Page page)
        {
            this.currentPage = page;
            string htmlToReturn = "";

            int languageId = Convert.ToInt32(page.language_id);
            IPageDAO PageDAO = new Models.DAO.PageDAO();
            IVariableValueDAO VariableValueDAO = new Models.DAO.VariableValueDAO();
            IGalleryImageDAO galleryImageDAO = new Models.DAO.GalleryImageDAO();
            IQueryable<Page> PagesCollection = PageDAO.FindByLanguageAndTypeNameAndPublished(Convert.ToInt32(page.language_id), Constants._newsPageTypeName, true);
            if (PagesCollection != null) {

                htmlToReturn += String.Format("<div id=\"implementationList\">");
                htmlToReturn += String.Format("<table>");
                htmlToReturn += String.Format("<thead>");
                htmlToReturn += String.Format("<tr>");
                htmlToReturn += String.Format("<td>" + translationHelper.getTranslation("_newsFilterNumber", languageId) + "</td>");
                htmlToReturn += String.Format("<td>" + translationHelper.getTranslation("_newsType", languageId) + "</td>");
                htmlToReturn += String.Format("<td>" + translationHelper.getTranslation("_newsMainApplication", languageId) + "</td>");
                htmlToReturn += String.Format("<td class=\"last\">" + translationHelper.getTranslation("_newsSpecification", languageId) + "</td>");
                htmlToReturn += String.Format("</tr>");
                htmlToReturn += String.Format("</thead>");
                htmlToReturn += String.Format("<tbody>");

                foreach (Page singlePage in PagesCollection) {
                    string galleryName = "newsPDF";
                    string pdfLink = String.Empty;
                    IQueryable<GalleryImage> galleryImages = galleryImageDAO.FindByGalleryNameAndPage(galleryName, singlePage.page_id);
                    if (galleryImages != null)
                    {
                        foreach (GalleryImage singleImage in galleryImages)
                        {
                            pdfLink += singleImage.gallery_image_link;
                        }
                    }

                    VariableValue variableDate = VariableValueDAO.FindByVariableAndPage(Constants._newsDateVariableName, singlePage.page_id, Convert.ToInt32(page.language_id));
                    VariableValue pageDate = VariableValueDAO.FindByVariableAndPage(Constants._newsBoxSingleScope, page.page_id, Convert.ToInt32(page.language_id));
                    if (pageDate.variable_value == variableDate.variable_value) {

                        VariableValue filterNumber = VariableValueDAO.FindByVariableAndPage(Constants._newsFilterNumber, singlePage.page_id, Convert.ToInt32(page.language_id));
                        VariableValue filterType = VariableValueDAO.FindByVariableAndPage(Constants._newsFilterType, singlePage.page_id, Convert.ToInt32(page.language_id));
                        VariableValue application = VariableValueDAO.FindByVariableAndPage(Constants._newsApplication, singlePage.page_id, Convert.ToInt32(page.language_id));

                        htmlToReturn += String.Format("<tr>");
                        htmlToReturn += String.Format(" <td><strong>{0}</strong></td>", filterNumber.variable_value);
                        htmlToReturn += String.Format(" <td>{0}</td>", filterType.variable_value);
                        htmlToReturn += String.Format(" <td>{0}</td>", application.variable_value);
                        htmlToReturn += String.Format(" <td class=\"last\"><a href=\"" + pdfLink + "\">" + translationHelper.getTranslation("_downloadPDF", languageId) + " <img src=\"../../../Content/images/pdfico.gif\" alt=\"pdf\" /></a></td>");
                        htmlToReturn += String.Format("</tr>");
                        
                    }
                }

                htmlToReturn += String.Format("</tbody>");
                htmlToReturn += String.Format("</table>");
                htmlToReturn += "</div>";

            }

            return htmlToReturn;
        }
    }
}
